package com.dataseek.iot.core.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cglib.beans.BeanCopier;

import java.util.HashMap;
import java.util.Map;

/**
 * @author cc
 * @Title: copy工具类
 * @Description: copy工具类
 * @date 2021/9/173:42 下午
 * @since 1.0
 */
@Slf4j
public class CopyUtil {
    private static final Map<String, BeanCopier> beanCopierMap = new HashMap<String, BeanCopier>();

    public static void copyPropertiesCglib(Object source, Object target){
        String beanKey = generateBeanKey(source.getClass(), target.getClass());
        BeanCopier copier = null;
        if(!beanCopierMap.containsKey(beanKey)){
            copier = BeanCopier.create(source.getClass(), target.getClass(), false);
            beanCopierMap.put(beanKey, copier);
        }else {
            copier = beanCopierMap.get(beanKey);
        }
        copier.copy(source, target, null);
    }

    public static String generateBeanKey(Class<?> source, Class<?> target){
        return source.getName() + "@" + target.getName();
    }
}
